<?php
/**
* UserErrorHandler.php
*
* Logging functions.
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once("config.php");
require_once('Mail.php');
require_once('Mail/mime.php');

/// {{{ Error handling 
// we will do our own error handling

class UserErrorHandler
{
    /**
     * Class c'tor
     */
    function UserErrorHandler( )
    {
        //Register function for error handling
        $old_error_handler = set_error_handler(array("userErrorHandler","handler"));
        trigger_error("LEAVE UserErrorHandler::userErrorHandler() ", E_USER_NOTICE);
    }

    /**
     * Custom error handler
     */
    function handler($errno, $errmsg, $filename, $linenum, $vars)
    {
        $options = &PEAR::getStaticProperty('CONFIG_INI','DEBUG'); 

        //get configuration data from global configuration ;
        // timestamp for the error entry
        $dt = date("Y-m-d H:i:s");

        // define an assoc array of error string
        // in reality the only entries we should
        // consider are E_WARNING, E_NOTICE, E_USER_ERROR,
        // E_USER_WARNING and E_USER_NOTICE
        $errortype = array (
                    E_ERROR              => 'Error',
                    E_WARNING            => 'Warning',
                    E_PARSE              => 'Parsing Error',
                    E_NOTICE             => 'Notice',
                    E_CORE_ERROR         => 'Core Error',
                    E_CORE_WARNING       => 'Core Warning',
                    E_COMPILE_ERROR      => 'Compile Error',
                    E_COMPILE_WARNING    => 'Compile Warning',
                    E_USER_ERROR         => 'User Error',
                    E_USER_WARNING       => 'User Warning',
                    E_USER_NOTICE        => 'User Notice',
                    E_STRICT             => 'Runtime Notice',
                    E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
                    );

        $err  = "";
        $err .= "" . $dt . "";
        $err .= "\t" . $errno . "";
        $err .= "\t" . $errortype[$errno] . "";
        $err .= "\t" . $errmsg . "";
        $err .= "\t" . $filename . "";
        $err .= "\t" . $linenum . "";
        $err .= "\n";
       
        if ( $options["debug"] ) 
        {
            echo $err;
        } 
        else
        {

            // save to the error log, and e-mail me if there is a critical user error
            
            $options = &PEAR::getStaticProperty('CONFIG_INI','DEBUG');
            $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
            
            if ( $errno & ((int)$options['error_reporting']) ) {
                UserErrorHandler::errorLog($err,$options["log_file"]);
            }

            if ($errno == E_USER_ERROR  ) 
            {
                if ( !$options['send_error'] )
                {
                    die("Critical User Error ! Report was not send to administrator");
                }

                $hdrs = array(
                              'From'    => $global['email'],
                              'Subject' => 'Critical User Error !.'
                              );

                $mime = new Mail_mime();

                $mime->setTXTBody($err);

                $body = $mime->get();
                $hdrs = $mime->headers($hdrs);

                $params = array ( 'host' => $global['host'],
                                  'auth'=>true,
                                  'username'=>$global['username'],
                                  'password'=>$global['password'],
                                  'port'=>$global['port']);
                $mail =& Mail::factory('smtp',$params);

                if(PEAR::isError($mail))
                {
                   trigger_error("UserErrorHandler::handler() ".$mail->getMsg(), E_USER_ERROR); 
                }

                $status = $mail->send($global["email"], $hdrs, $body);

                if(PEAR::isError($status))
                {
                   trigger_error("UserErrorHandler::handler() ".$status->getMessage(), E_USER_ERROR); 
                }
                die("Critical User Error ! Report was send to administrator");
            }
        }
    }

    /**
     * Custom error log
     */
    static function errorLog($err,$file)
    {   

        if (is_writable($file) || !file_exists($file) ) {

            if (!$handle = fopen($file, 'a')) {
                trigger_error("UserErrorHandler::errorLog(): Could not create file.", E_USER_ERROR);
                return;
            }  


            if (fwrite($handle, $err) === FALSE) {
                trigger_error("UserErrorHandler::errorLog(): Could not write to file.", E_USER_ERROR);
                return;
            }
            fclose($handle);
        }
        else
        {
            trigger_error("UserErrorHandler::errorLog(): File read only.", E_USER_ERROR);
        }
    }   
}
//Initialize error handling;
$error = new UserErrorHandler;
/// }}} 

?>
